package action;

import config.ApplicationConfig;
import net.sf.json.JSONObject;
import server.HistoryServer;
import server.LoginServer;
import server.SettingServer;
import util.Decode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-03-10 Sunday 15:09:34
 */
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        session.setAttribute("infoTitle", "用户登录");
        session.setAttribute("panelColor", "blue");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String verificationCode = request.getParameter("ver_code");
        String returnPage = request.getParameter("return");
        String cookie = session.getAttribute("cookie").toString();

        JSONObject result = JSONObject.fromObject("{}");
        if (username != null && !username.equals("") &&
                password != null && !password.equals("") &&
                verificationCode != null && !verificationCode.equals("") &&
                returnPage != null && !returnPage.equals("") &&
                cookie != null && !cookie.equals("")) {

            Decode decode = new Decode();
            username = decode.urlUtf8Decode(username);
            password = decode.urlUtf8Decode(password);
            verificationCode = decode.urlUtf8Decode(verificationCode);
            returnPage = decode.urlUtf8Decode(returnPage);
            cookie = decode.urlUtf8Decode(cookie);

            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (loginServer.checkLogin(username,cookie)) {
                loginServer.deleteLogin(username);
                loginServer.record(username,request);
            }
            result = loginServer.login(username, password, cookie, verificationCode);
            if (result.getString("state").equals("true")) {
                session.setAttribute("user", username);
                loginServer.record(username, request);
            } else {
                session.setAttribute("panelColor", "red");
            }
            session.setAttribute("infoContent", result.getString("content"));
            session.setAttribute("return", returnPage);
            loginServer.close();
        } else {
            session.setAttribute("panelColor", "red");
            session.setAttribute("infoContent", "登录失败，信息不能为空");
            session.setAttribute("return", "index");
        }
        HistoryServer historyServer = new HistoryServer();
        historyServer.open();
        historyServer.record(result.has("userID") ? result.getString("userID") : "", username + "尝试登录" + (result.has("content") ? "，"+result.getString("content") : "登录失败，原因未知"), request);
        historyServer.close();
        response.sendRedirect("information.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
